$include("$theme/feedViewContainer.css");

$import("fp.views.FeedExploreView");
$import("fp.views.FeedListView");

with ($ns("fp.controls"))
{
    fp.controls.FeedViewContainer = function($p_container, p_options)
    {
        var me = new mx.controls.JQControl($p_container, p_options);
        var base = {};
        
        
        me.path = null;
        
        me.activeView = null;
        me.views = {};
        
        me.isLoading = false;
        
        
        
        me.onloading = null;
        me.onreloading = null;
        
        
        _$mask = $("<div id='mask'>");
        var _$loadingImage = $("<span id='loadingImage' />");
        var _$scrollToTop = $("<div id='scrollToTop'><img src='" + $mappath("~/icons/arrow_up_64.png") + "'/></div>");
        
        
        
        base.init = me.init;
        me.init = function()
        {
            base.init();
            
            me.$container.addClass("feedViewContainer");
            $(window).keydown(_onkeydown);
            
            me.views["explore"] = new fp.views.FeedExploreView();
            me.views["list"] = new fp.views.FeedListView();
            me.switchView(fp.Preferences.get("feedViewContainer.viewType", "explore"));
            
            
            
            _$mask.click(function(e) { me.activeView.trigger("maskhiding"); })
            
            _$scrollToTop.click(me.activeView.moveFirst);
            me.$container.append(_$scrollToTop);
        };
        
        
        
        me.switchView = function(p_viewType, p_reload)
        {
            $("body > #main > #top > .toolbar > li.selected").removeClass("selected");
            $("body > #main > #top > .toolbar > li#" + p_viewType).addClass("selected");
            
            if (me.activeView != null)
            {
                if (me.activeView.viewType == p_viewType)
                {
                    return;
                }
                
                me.activeView.deactivate();
                me.activeView.$container.parent().detach();
            }
            me.activeView = me.getView(p_viewType);
            me.$container.append(me.activeView.$container.parent());
            me.activeView.activate();
            
            if (p_reload)
            {
                me.reload();
            }
            
            fp.Preferences.set("feedViewContainer.viewType", p_viewType);
        };
        
        me.getView = function(p_viewType)
        {
            var view = me.views[p_viewType];
            if (!view.initialized)
            {
                view.bind("loadingrequired", _activeView_loadingrequired);
                view.bind("maskshowing", _activeView_maskshowing);
                view.bind("maskhiding", _activeView_maskhiding);
                view.init();
            }
            return view;
        };
        
        
        
        me.continuation = null;
        me.load = function()
        {
            if (me.continuation == "END")
            {
                return;
            }
            
            me.isLoading = true;
            me.trigger("loading");
            g.Reader.getFeeds({
                path: me.path,
                count: me.activeView.pageCount,
                continuation: me.continuation,
                success: function(p_result)
                {
                    me.isLoading = false;
                    _stopSpinning();
                    
                    _loadFeeds_callback(p_result);
                },
                error: function()
                {
                    me.isLoading = false;
                    _stopSpinning();
                }
            });
        };
        
        me.reload = function()
        {
            _$loadingImage.css("backgroundImage", "url(" + $mappath("$theme/images/spinner.png") + ")");
            _startSpinning();
            
            me.continuation = null;
            me.activeView.clearFeeds();
            
            me.trigger("reloading");
            me.load();
        };
        
        
        
        
        me.showMask = function()
        {
            $("body > #main > #top > .toolbar, body > #main > #top > #path").fadeOut();
            me.$container.append(_$mask);
            _$mask.opacity(0.8);
            _$mask.show();
        };
        
        me.hideMask = function()
        {
            $("body > #main > #top > .toolbar, body > #main > #top > #path").fadeIn();
            _$mask.detach();
        };
        
        
        
        
        
        
        function _loadFeeds_callback(p_result)
        {
            if (p_result.continuation)
            {
                me.continuation = p_result.continuation;
            }
            else
            {
                me.continuation = "END";
            }
            me.activeView.appendFeeds(p_result.items);
        }
        
        function _activeView_loadingrequired(e)
        {
            if (me.isLoading) return;
            
            me.load();
        }
        
        function _activeView_maskshowing(e)
        {
           me.showMask();
        }
        
        function _activeView_maskhiding(e)
        {
            me.hideMask();
        }
        
        function _onkeydown(e)
        {
            if (e.keyCode == 49)
            {
                me.switchView("explore", true);
            }
            else if (e.keyCode == 50)
            {
                me.switchView("list", true);
            }
            else if ((e.keyCode == 75 && e.shiftKey) || (e.keyCode == 73 && !e.shiftKey))
            {
                me.activeView.moveFirst();
            }
            else if ((e.keyCode == 75 && !e.shiftKey) || (e.keyCode == 32 && e.shiftKey))
            {
                me.activeView.movePrevious();
            }
            else if (e.keyCode == 74 || (e.keyCode == 32 && !e.shiftKey))
            {
                me.activeView.moveNext();
            }
            else if (e.keyCode == 83)
            {
                me.activeView.toggleStar();
            }
            else if (e.keyCode == 86)
            {
                me.activeView.openInNewTab();
            }
            else if (e.keyCode == 229)
            {
                // Pinyin
            }
        }
        
        
        var _spinningTimer = null;
        var _spinningFrameIndex = 0;
        function _startSpinning()
        {
            _$loadingImage.css("left", (me.$container.width() - 42) / 2);
            _$loadingImage.css("top", (me.$container.height() - 42) / 2);
            me.$container.append(_$loadingImage);
            
            _spinningFrameIndex = 0;
            _spinningTimer = setInterval(function(){
                _$loadingImage.css("backgroundPositionY", -_spinningFrameIndex * 42);
                _spinningFrameIndex++;
                if (_spinningFrameIndex == 12)
                {
                    _spinningFrameIndex = 0;
                }
            }, 150);
        }
        
        function _stopSpinning()
        {
            clearInterval(_spinningTimer);
            _$loadingImage.detach();
        }
        
        return me;
    };
}